<template>
  <div class="reg-container">
    <!-- 头部的 logo 区域 -->
    <div class="header">
      <img src="@/assets/images/logo.png" alt="" />
    </div>

    <!-- 登录和注册区域 -->
    <div class="box">
      <div class="box-header"></div>
      <!-- 登录的表单 -->
      <!-- <el-form>
        <el-form-item>
          <el-input
            placeholder="请输入用户名"
            maxlength="10"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item >
          <el-input
            placeholder="请输入密码"
            maxlength="15"
            type="password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="btn-login">登录</el-button>
        <el-link type="info" class="link-reg">去注册账号</el-link>
      </el-form> -->
   <el-form ref="formRef" :model="regForm" :rules="rule">
     <el-form-item prop="username">
       <el-input v-model="regForm.username" placeholder="请输入用户名"  prefix-icon="el-icon-user-solid"></el-input>
     </el-form-item>
     <el-form-item prop="password">
       <el-input v-model="regForm.password" show-password placeholder="请输入密码" prefix-icon='el-icon-lock' type='password'></el-input>
     </el-form-item >
     <el-form-item prop="repassword">
       <el-input v-model="regForm.repassword" show-password placeholder="请再次输入密码" prefix-icon='el-icon-lock' type='password'></el-input>
     </el-form-item >
   </el-form>
   <el-button type="primary" class="btn-login" @click="register" >注册</el-button>
   <el-link type="info" class="link-reg" @click="$router.push('/login')">去登录</el-link>
    </div>
  </div>
</template>
<script>
import { reqReg } from '@/api/user'
// import axios from '@/utils/request'
// import { reqReg } from '@/api/user'
export default {
  name: 'Reg',
  data () {
    const regRepass = (rule, value, callback) => {
      if (value !== this.regForm.password) {
        callback(new Error('两次密码不一致'))
      } else {
        callback()
      }
    }

    return {
      regForm: {
        username: '',
        password: '',
        repassword: ''
      },
      rule: {
        username: [
          { required: true, message: '用户名不能为空', trigger: ['change', 'blur'] },
          { pattern: /^[0-9a-zA-Z]{1,10}$/, message: '用户名不正确', trigger: ['change', 'blur'] }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: ['change', 'blur'] },
          { pattern: /^\S{6,15}$/, message: '密码不正确', trigger: ['change', 'blur'] }
        ],
        repassword: [
          { required: true, message: '密码不能为空', trigger: ['change', 'blur'] },
          { pattern: /^\S{6,15}$/, message: '密码不正确', trigger: ['change', 'blur'] },
          { validator: regRepass, trigger: ['change', 'blur'] }
        ]

      }
    }
  },
  methods: {
    register () {
      this.$refs.formRef.validate(async (flag) => {
        if (!flag) return
        console.log('发请求了')
        const { data } = await reqReg(this.regForm)
        console.log(data)
        if (data.code !== 0) {
          return this.$message.error('用户名已被注册')
        }
        this.$message.success('注册成功')
        this.$router.push('/login')
      })
    }
  }

}
</script>
<style lang="less" scoped>
.reg-container {
  background-color: blue;
  height: 100%;
  background: url('~@/assets/images/login_bg.jpg') no-repeat center;
  background-size: cover;

  .header {
    width: 1200px;
    margin: 0 auto;
    user-select: none;
  }

  .box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 3px;
    box-shadow: 0 1px 15px rgba(0, 0, 0, 0.2);
    padding: 0 30px;
    box-sizing: border-box;

    .box-header {
      height: 60px;
      background: url('~@/assets/images/login_title.png') no-repeat center;
    }

    .btn-login {
      width: 100%;
    }

    .link-reg {
      font-size: 12px;
    }
  }
}
</style>
